<template>
  <div>
    <el-form label-position="left" label-width="160px" style="width:100%;">
      <el-form-item label="摄像头编号："><el-input v-model="formData.cameraNo" placeholder="请输入摄像头编号" class="filter-item" /></el-form-item>
      <el-form-item label="部署类型：">
        <el-radio-group v-model="formData.installType">
          <el-radio-button label="0">入口</el-radio-button>
          <el-radio-button label="1">途径点</el-radio-button>
          <el-radio-button label="2">出口</el-radio-button>
          <el-radio-button label="3">出入口</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="蓄车场：">
        <el-radio-group v-model="formData.parkingNo">
          <el-radio-button label="0">南蓄</el-radio-button>
          <el-radio-button label="1">北蓄</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="摄像机类型：">
        <el-radio-group v-model="formData.cameraType">
          <el-radio-button label="0">牌照识别摄像机</el-radio-button>
          <el-radio-button label="1">计数摄像机</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="摄像头物理位置说明："><el-input v-model="formData.installPlace" placeholder="请输入摄像头物理位置说明" class="filter-item" /></el-form-item>
      <el-form-item label="IP："><el-input v-model="formData.ip" placeholder="请输入IP" class="filter-item" /></el-form-item>
      <el-form-item label="端口："><el-input-number v-model="formData.port" placeholder="请输入端口" controls-position="right" class="filter-item" /></el-form-item>
      <el-form-item label="用户名："><el-input v-model="formData.userID" placeholder="请输入用户名" class="filter-item" /></el-form-item>
      <el-form-item label="密码："><el-input v-model="formData.password" placeholder="请输入密码" class="filter-item" /></el-form-item>
      <el-form-item label="照片存储路径："><el-input v-model="formData.photoPath" placeholder="请输入照片存储路径" class="filter-item" /></el-form-item>
      <el-form-item label="状态：">
        <el-radio-group v-model="formData.status">
          <el-radio-button label="0">使用</el-radio-button>
          <el-radio-button label="1">停用</el-radio-button>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button icon="el-icon-close" @click="cancel">取消</el-button>
      <el-button v-waves :loading="confirmLoading" type="primary" icon="el-icon-check" @click="submit">确定</el-button>
    </div>
  </div>
</template>
<script>
import { getSysCameraDetail, addSysCamera, editSysCamera } from '@/api/camera';
import waves from '@/directive/waves';

export default {
  name: 'SysCameraAdd',
  directives: { waves },
  props: {
    id: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      confirmLoading: false,
      formData: {
        installType: null,
        parkingNo: null,
        cameraType: null,
        status: null
      }
    };
  },
  created() {
    if (this.id) {
      this.getDetail(this.id);
    } else {
      this.formData.installType = 0;
      this.formData.parkingNo = 0;
      this.formData.cameraType = 0;
      this.formData.status = 0;
    }
  },
  methods: {
    async getDetail(id) {
      await getSysCameraDetail(id).then(response => {
        this.formData = response.data;
      });
    },
    async submit() {
      if (this.formData.cameraNo === '') {
        this.$message.error('请输入摄像头编号');
        return;
      }
      this.confirmLoading = true;
      if (this.id) {
        await editSysCamera(this.formData).then(res => {
          this.confirmLoading = false;
          if (res.msg !== 'OK') {
            this.$message.error(res.msg);
            return;
          }
          this.$message.success('编辑成功');
          this.$emit('cancelClose', true, 'edit');
        });
      } else {
        await addSysCamera(this.formData).then(res => {
          this.confirmLoading = false;
          if (res.msg !== 'OK') {
            this.$message.error(res.msg);
            return;
          }
          this.$message.success('新增成功');
          this.$emit('cancelClose', true, 'add');
        });
      }
    },
    cancel() {
      if (this.id) {
        this.$emit('cancelClose', false, 'edit');
      } else {
        this.$emit('cancelClose', false, 'add');
      }
    }
  }
};
</script>
